package org.example.student_dormitory.controller;

import net.sf.json.JSONObject;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.service.StudentService;
import org.example.student_dormitory.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/student")
public class StudentManagerServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求响应编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        // 获取前端请求数据
        String method = req.getParameter("method");
        switch (method){
            case "paging" ://分页
                paging(req,resp);
                break;
            case "loadBuilding"://加载楼栋
                loadbuildings(req,resp);
                break;
            case "loadDormitory":
                loadDormitorys(req,resp);
                break;
            case "add":
                addStudents(req,resp);
                break;
        }
    }

    private void addStudents(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String number = req.getParameter("number");
        String stuName = req.getParameter("stu_name");
        String gender = req.getParameter("gender");
        System.out.println(gender);
        String buildingId = req.getParameter("buildingId");
        String dormitoryId = req.getParameter("dormitoryId");
        System.out.println(dormitoryId);

//        System.out.println("1准备进入Service");
        Result result = studentService.addStudent(number,stuName,buildingId,dormitoryId,gender);
//        System.out.println("7转前端");
        returnData(resp,result);
    }

    private void loadDormitorys(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取宿舍信息

        String buildingId = req.getParameter("buildingId");
        Result result = studentService.loadDormitory(buildingId);

        returnData(resp,result);
    }

    private void loadbuildings(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //加载楼栋信息
        Result result = studentService.loadBuildings();
        returnData(resp,result);
    }

    private void paging(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //分页
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        String key = req.getParameter("key");
        String value = req.getParameter("value");
        //
        System.out.println("1准备进入Service");
        Result result = studentService.loadStudent(page, pageSize, key, value);
        returnData(resp,result);
    }

    private void returnData(HttpServletResponse resp, Result result) throws IOException {
        String json_str = JSONObject.fromObject(result).toString();

        resp.getWriter().write(json_str);
    }
}
